Java 15 Features Intro
π Java 15 β New Features With a Twist of Humor ββ
Release Date: September 15th, 2020
Frequency: Every 6 months, like clockwork.
Mood: Still Java, but cooler.
Brace yourself, brave developer, for Java 15 is not just a numberβitβs a tale of sealed secrets, invisible classes, and rebellious record-keepers. Grab your virtual coffee, and let's dive in! π
1. π‘οΈ Sealed Classes and Interfaces (Preview) β JEP 360β
Before Java 15, any class could walk up to your public class and say, βYo, Iβm your child now.β Not anymore!
Enter sealed classes and interfaces, the bouncers of Java inheritance. Using the sealed
keyword, you can now whitelist the classes allowed to extend or implement your base class/interface. Total control, baby. β
Here's how the VIP list looks:
sealed class Account
permits CurrentAccount, SavingAccount, LoanAccount {
}
final class CurrentAccount extends Account {}
non-sealed class SavingAccount extends Account {}
sealed class LoanAccount extends Account permits HomeloanAccount, AutoloanAccount {}
final class HomeloanAccount extends LoanAccount{}
final class AutoloanAccount extends LoanAccount{}
Translation:
sealed
= βYou canβt sit with usβ¦ unless youβre on the list.βfinal
= βEnd of the line.βnon-sealed
= βFree spirit. Do what you want.β
π Read More: Sealed Classes and Interfaces
2. π EdDSA Algorithm β JEP 339β
Need a signature thatβs both faster and more secure than your doctorβs? Say hello to EdDSA (Edwards-Curve Digital Signature Algorithm)!
KeyPairGenerator kpg = KeyPairGenerator.getInstance("Ed25519");
KeyPair kp = kpg.generateKeyPair();
byte[] msg = "test_string".getBytes(StandardCharsets.UTF_8);
Signature sig = Signature.getInstance("Ed25519");
sig.initSign(kp.getPrivate());
sig.update(msg);
byte[] s = sig.sign();
String encodedString = Base64.getEncoder().encodeToString(s);
System.out.println(encodedString);
It's like a digital wax seal for your data. Fancy AND secure. π
π Read More: Java EdDSA (Ed25519 / Ed448) Example
3. πΆοΈ Hidden Classes β JEP 371β
These arenβt just shy classes. Theyβre invisible ninjas of the JVM. π₯·
Hidden classes canβt be accessed by other classes' bytecode and are perfect for frameworks that create classes at runtime.
Why hide them?
- So your framework internals donβt clutter the JVM.
- So you can unload them guilt-free.
- So you donβt have to rely on that sketchy
sun.misc.Unsafe
guy anymore.
4. π Other Enhancements (Cool Stuff Corner)β
4.1 π§© Pattern Matching for instanceof
(Second Preview) β JEP 375β
Less boilerplate. More magic. You can now combine type checks and casting in a single line. π―
if (obj instanceof String s) {
// s is now a String you can use directly. No casting. Woohoo!
}
// Real-world example
if(customer instanceof PersonalCustomer pCustomer) {
customerName = String.join(" ",
pCustomer.getFirstName(),
pCustomer.getMiddleName(),
pCustomer.getLastName());
} else if(customer instanceof BusinessCustomer bCustomer) {
customerName = bCustomer.getLegalName();
}
π Read More: Pattern Matching for instanceof
4.2 πͺ¦ Removed Nashorn JavaScript Engine β JEP 372β
First introduced in Java 8, Nashorn was Javaβs answer to βHey, letβs do JS too!β But ECMAScript evolves faster than a Java developer rejecting frontend tasks. π
So Nashornβs out. Bye bye, JS in JVM. π
4.3 π‘ Reimplement Legacy DatagramSocket API β JEP 373β
The DatagramSocket API got a glow-up! β¨
- Easier debugging
- Friendly with upcoming virtual threads
- Helps Project Loom nap better π΄
4.4 π§Ύ Records (Second Preview) β JEP 384β
Records are Javaβs way of saying: βLetβs stop writing boilerplate and just store some dang data!β
record User(String name, int age) {}
Boom! You get:
constructor
getters
equals()
hashCode()
toString()
And yes, trying to mutate fields via reflection now throws an IllegalAccessException
. Sorry hackers! π«
4.5 π§΅ Text Blocks β JEP 378β
Multi-line strings are here, and theyβre beautiful.
No more \n
, no more +
, just triple quotes and peace. βοΈ
String dbSchema = """
CREATE TABLE 'TEST'.'EMPLOYEE'
(
'ID' INT NOT NULL DEFAULT 0 ,
'FIRST_NAME' VARCHAR(100) NOT NULL ,
'LAST_NAME' VARCHAR(100) NULL ,
'STAT_CD' TINYINT NOT NULL DEFAULT 0
);
""";
π Read More: Text Blocks
4.6 ποΈ Garbage Collection β JEPs 377 & 379β
- ZGC and Shenandoah are now official, not just experimental. π§ͺβ
- G1 is still the default.
Javaβs garbage collectors just keep getting smarter (unlike your roommate).
5. π Conclusionβ
Java 15 brings us:
- π‘οΈ Sealed classes
- πΆοΈ Hidden classes
- π§Ύ Records
- π§΅ Text blocks
- π EdDSA signatures
- π Goodbye Nashorn
- π§ Smarter pattern matching
- ποΈ Upgraded GCs
Whether youβre a curious coder or a battle-hardened Java warrior, Java 15 has a little something for everyone.
Stay sealed, hidden, and immutable, my friends. βπ»
Happy Learning!! π